﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Agile.Paypal.Web.Mvc
{
    public static class HtmlHelperExtensions
    {
        public static PaypalHtmlHelper Paypal(this HtmlHelper htmlHelper)
        {
            return new PaypalHtmlHelper(htmlHelper);
        }
    }

    public class PaypalHtmlHelper
    {
        private readonly HtmlHelper _htmlHelper;

        public PaypalHtmlHelper(HtmlHelper htmlHelper)
        {
            _htmlHelper = htmlHelper;
        }

        public IHtmlString Buy(PaypalBuyRequest args)
        {
            string url = !args.TestEnviroment ? "https://www.paypal.com/cgi-bin/webscr" : "https://www.sandbox.paypal.com/cgi-bin/webscr";


            _htmlHelper.Raw(string.Format("<form action=\"{0}\" method=\"post\">", url));

            _htmlHelper.Raw(string.Format("<INPUT TYPE=\"hidden\" name=\"{0}\" value=\"{1}\" />", "cmd", "_xclick"));
            _htmlHelper.Raw(string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "amount", decimal.Round(request.Amount, 2).ToString(CultureInfo.InvariantCulture)));


            _htmlHelper.Raw(string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "no_shipping", 1));
            _htmlHelper.Raw(string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "rm", 2));
            _htmlHelper.Raw(string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "business", _commerce.Email));
            _htmlHelper.Raw(string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "item_name", request.ProductName));
            _htmlHelper.Raw(string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "currency_code", "EUR");

            // _htmlHelper.Raw("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "notify_url", GetNotificationUrl());
            _htmlHelper.Raw(string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "custom", request.CustomData));
            _htmlHelper.Raw(string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "return", request.OnSuccess));
  
            _htmlHelper.Raw(string.Format("<input type=\"submit\" value=\"" + Localization.Here + "\">");
            _htmlHelper.Raw(string.Format("</form>");
        }
    }
}
